package supercafe.core;

import java.util.Vector;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageButton;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MenuItemsScreen extends ListActivity {
	TextView selection;
	Vector<MenuItem> listMenuItems;
	String[] itemNames;
	MenuItemListAdapder adap;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu_items_screen);
        try{
        	
        	String title = getIntent().getExtras().getString("title");
        	TextView tvTitle = (TextView) findViewById(R.id.menuTitle);
        	tvTitle.setText(title);
        	
        listMenuItems = new Vector<MenuItem>();
        listMenuItems.add(new MenuItem(1,"Cafe sữa",100000));
        listMenuItems.add(new MenuItem(2,"Cafe đen",120000));
        listMenuItems.add(new MenuItem(3,"Trà sữa",130000));
        listMenuItems.add(new MenuItem(5,"Trà đen",140000));
        
        /*
        ArrayAdapter<String> aa = new ArrayAdapter<String>(this, 
                //android.R.layout.select_dialog_multichoice,
        		R.layout.list_menu_items_adapter_content,
        		R.id.menuItemName,
                itemNames);
        */
        adap = new MenuItemListAdapder(this,R.layout.list_menu_items_row,listMenuItems);
        
        
        selection = (TextView) findViewById(R.id.tvSelections);
        setListAdapter(adap); 
        
        }catch(Exception e){
        	Toast.makeText(this, "exception = "+e.getMessage() +"\n"+e.getStackTrace()[0].toString()+"\n"+"\n"+e.getStackTrace()[1].toString()+"\n"+"\n"+e.getStackTrace()[2].toString()+"\n"+"\n"+e.getStackTrace()[3].toString()+"\n", 3000).show();
        }
        
	}
	
	public void onListItemClick(ListView parent, View v, int position,
	             long id) { 
		//this.listMenuItems.get(position).setSelected(!this.listMenuItems.get(position).isSelected());
		//this.notifyDataSetChanged();
		Toast.makeText(this, "click on list item", 2000);
		
		selection.setText("Bạn đã chọn: ");
		int i=0;
		for(;i<listMenuItems.size();i++)
			//if(listMenuItems.get(i).isSelected())
			{
				selection.append(listMenuItems.get(i).getMenuItemName());
				break;
			}
		for(;i<listMenuItems.size();i++)
			//if(listMenuItems.get(i).isSelected())
			{
				selection.append(";"+listMenuItems.get(i).getMenuItemName());
			}
	}

	
	
	public static class MenuItemListAdapder extends BaseAdapter implements Filterable
	//, OnClickListener 
, OnClickListener
	{

		private Vector<MenuItem> listMenuItems;
		private LayoutInflater mInflater;
	    private Context context;
	    private int layoutId;
	    private int pos;
		
		public MenuItemListAdapder(Context context,
				int layoutId, Vector<MenuItem> listMenuItems) {
			// TODO Auto-generated constructor stub
			this.context = context;
			mInflater = LayoutInflater.from(context);
			this.layoutId = layoutId;
			this.listMenuItems = listMenuItems;
			
		}

		@Override
		public Filter getFilter() {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return this.listMenuItems.size();
		}

		@Override
		public Object getItem(int pos) {
			// TODO Auto-generated method stub
			return listMenuItems.get(pos);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return listMenuItems.get(position).getMenuItemId();
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			MenuItemHolder holder;
			if (convertView == null) {
		        convertView = mInflater.inflate(layoutId, null);

		        // Creates a ViewHolder and store references to the two children
		        // views
		        // we want to bind data to.
		        holder = new MenuItemHolder();
		        holder.menuItemName = (TextView) convertView.findViewById(R.id.menuItemName);
		        holder.menuItemPrice = (TextView) convertView.findViewById(R.id.menuItemPrice);
		        //holder.cbSelected = (CheckBox) convertView.findViewById(R.id.menuItemChecked);
		        /*
		        convertView.setOnClickListener(new OnClickListener() {
					int pos = position;
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						listMenuItems.get(position).setSelected(!listMenuItems.get(position).isSelected());
						v.setSelected(!v.isSelected());
						notifyDataSetChanged();
					}
				});
				*/
		        convertView.setOnClickListener(this);
		        //holder.cbSelected.setOnClickListener(this);
		        //holder.menuItemId.setOnClickListener(this);
		        //holder.menuItemName.setOnClickListener(this);
		        //holder.menuItemPrice.setOnClickListener(this);
		        
		        
		        
		        convertView.setTag(holder);
		      } else {
		        // Get the ViewHolder back to get fast access to the TextView
		        // and the ImageView.
		        holder = (MenuItemHolder) convertView.getTag();
		        
		      }

		      // Bind the data efficiently with the holder.
			  holder.menuItemId.setText(""+listMenuItems.get(position).getMenuItemId());
		      holder.menuItemName.setText(listMenuItems.get(position).getMenuItemName());
		      holder.menuItemPrice.setText(""+listMenuItems.get(position).getMenuItemPrice());
		      //holder.cbSelected.setSelected(listMenuItems.get(position).isSelected());
		      
		      holder.menuItemName.setTag(position);
		      holder.menuItemPrice.setTag(position);
		      holder.cbSelected.setTag(position);
		      
		      //holder.cbSelected.setTag(1,holder.cbSelected.isChecked());
			
			return convertView;
		}
		static class MenuItemHolder {
			TextView menuItemId;
	        TextView menuItemName;
	        TextView menuItemPrice;
	        CheckBox cbSelected;
	    }
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			//RelativeLayout line = (RelativeLayout) v.getParent();
			ListView line = (ListView) v.getParent();
			long[] IDs = line.getCheckItemIds();
			//TextView tvID = (TextView)line.getChildAt(0);
			//int id = Integer.parseInt(tvID.getText().toString());
			Toast.makeText(this.context, "id(s) = "+IDs.toString(), 2000);
		}
		
	}
	
}


